home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / cisco / account.shar / nameipacct.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-07  |  3.1 KB  |  182 lines

  1. #include <sys/param.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <arpa/inet.h>
  6. #include <netdb.h>
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include <sys/time.h>
  10. #include <errno.h>
  11. #include <ctype.h>
  12. #include <setjmp.h>
  13.  
  14. #include "nameipacct.h"
  15. /*   1.3 90/06/20 dfk@cwi.nl */
  16.  
  17. #define MAXRC 200        /* length reverse cache */
  18. #define DEFTOUTS 15
  19.  
  20. jmp_buf env;
  21. int timeout ();
  22. unsigned touts = 0;
  23.  
  24. extern char *netname();
  25.  
  26. extern char *optarg;
  27. extern int optind;
  28.  
  29. main(argc, argv)
  30.     int        argc;
  31.     char    *argv[];
  32. {
  33.     register int i;
  34.     char from[16];
  35.     char to[16];
  36.     char l[80];
  37.     int bytes;
  38.     int packets;
  39.  
  40.     while ((i=getopt(argc, argv, "t:")) != EOF)
  41.     {
  42.         switch(i)
  43.         {
  44.         case 't':
  45.             touts = atoi(optarg);
  46.             break;
  47.  
  48.         case '?':
  49.             fprintf(stderr, "usage: nameipacct [-t secs]\n");
  50.             exit(1);
  51.         }
  52.     }
  53.  
  54.     if (touts == 0)
  55.         touts = DEFTOUTS;
  56.     signal(SIGALRM, timeout);
  57.  
  58.     while (fgets(l, sizeof l, stdin) != NULL)
  59.     {
  60.         if (isdigit(l[0]))
  61.         {
  62.             sscanf(l, "%s %s %d %d\n", from, to, &packets, &bytes);
  63.             display(from, to, packets, bytes);
  64.         }
  65.         else
  66.             fputs(l, stdout);
  67.     }
  68. }
  69.  
  70. display(froma, toa, packets, bytes)
  71. char *froma;
  72. char *toa;
  73. int packets;
  74. int bytes;
  75. {
  76.     char from[40];
  77.     char to[40];
  78.     struct in_addr ha;
  79.     struct hostent *h;
  80.     register int len;
  81.  
  82.     ha.s_addr = inet_addr(froma);
  83.  
  84.     alarm(touts);
  85.     if (!setjmp(env) && !rchit(&ha) && (h=gethostbyaddr(&ha, 4, AF_INET)) != NULL)
  86.     {
  87.         len = strlen(h->h_name);
  88.         strncpy(from, len>29 ? h->h_name+(len-29) : h->h_name, sizeof(from));
  89.     }
  90.     else
  91.     {
  92.     rcenter(&ha);
  93.         sprintf(from, "%s", netname(ha));
  94.     }
  95.     alarm(0);
  96.         
  97.     ha.s_addr = inet_addr(toa);
  98.  
  99.     alarm(touts);
  100.     if (!setjmp(env) && !rchit(&ha) && (h=gethostbyaddr(&ha, 4, AF_INET)) != NULL)
  101.     {
  102.         len = strlen(h->h_name);
  103.         strncpy(to, len>29 ? h->h_name+(len-29) : h->h_name, sizeof(to));
  104.     }
  105.     else
  106.     {
  107.     rcenter(&ha);
  108.         sprintf(to, "%s", netname(ha));
  109.     }
  110.     alarm(0);
  111.         
  112.     /* print data */
  113.     printf("%-29.29s %-29.29s%8d%10d K\n", from, to, packets, bytes/1024);
  114. }
  115.  
  116. static unsigned rccurent = 0;
  117. static unsigned    rc[MAXRC];
  118.  
  119. rcenter(a)
  120. struct in_addr *a;
  121. {
  122.     if (rccurent < MAXRC)
  123.         rc[rccurent++] = (unsigned) a->s_addr;
  124. }
  125.  
  126. rchit(a)
  127. struct in_addr *a;
  128. {
  129.     register int i;
  130.     register unsigned aa;
  131.  
  132.     aa = (unsigned) a->s_addr;
  133.     for (i=0; i<rccurent; i++)
  134.     {
  135.         if (rc[i] == aa)
  136.             return(1);
  137.     }
  138.     
  139.     return(0);
  140. }
  141.  
  142. timeout ()
  143. {
  144.  
  145.     alarm(0);
  146.     longjmp(env, 1);
  147. }
  148.     
  149.  
  150.  
  151. char *
  152. netname(ha)
  153. struct in_addr ha;
  154. {
  155.     static char cmd[80];
  156.     static char buf[80];
  157.     static char quadname[16];
  158.     register FILE *pipe;
  159.     register char *p;
  160.  
  161.     strncpy(quadname, inet_ntoa(ha), sizeof quadname);
  162. #ifdef RIPEDBCMD
  163.         sprintf(cmd, "%s %s", RIPEDBCMD, inet_ntoa(inet_makeaddr(inet_netof(ha),0)));
  164.     if ((pipe=popen(cmd, "r")) == NULL)
  165.         return(quadname);
  166.     
  167.     while (fgets(buf, sizeof buf, pipe) != NULL)
  168.     {
  169.         if (strncmp(buf, "*na: ", 5) == 0)
  170.         {
  171.             for (p=buf+strlen(buf)-1; isspace(*p); p--)
  172.                 *p = 0;
  173.             sprintf(cmd, "%s-%s", buf+5, quadname);
  174.             return(cmd);
  175.         }
  176.     }
  177. #else RIPEDBCMD
  178.     return(quadname);
  179. #endif RIPEDBCMD
  180. }
  181.             
  182.